package API;

import Util.MyKey;
import Util.SignEncrypt;
import org.bouncycastle.util.encoders.Base64;


import java.net.URLDecoder;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Map;

import static Util.Tools.getURLParameters;


/**
 * 快捷支付异步通知处理 文档地址：https://open.heepay.com/www/index.html#/openDoc?type=menu&id=2022210140141152
 * 接收的参数值，只需要UrlDecode一次，如果开发平台默认UrlDecode
 * 用户支付后同步返回订单的return_url地址，异步返回的订单notify_url地址，支付是否成功需要获取请求参数status中的值，对于异步通知处理需要在商户端完成订单处理后，需要根据订单的校验信息是否正确返回"ok"或者"error"字符串，不能有其他任何输出（包括空格、空行）。
 * 通知的参数只有三个参数agent_id、encrypt_data和sign，解密encrypt_data，验签sign
 * 验证签名：encrypt_data解密后的参数拼接验签串、汇付宝公钥和sign参与验签
 */

public class KjNotify {

    public static void main(String[] args) throws Exception {

        //异步通知的完整url+内容，自己服务器获取到赋值给PayNotify
        String Notify = "https://pay.heepay.com/payNotify?agent_id=1664502&encrypt_data=aaklIpzTuYtXeKVToduFQS6P1W4RapPFtD1kqGF4Ho7JpA2N%2f9r5QsPHwmMmgvL1iKDzU96cOK4F8YvlhVkice2pIbKnJZpNQMtFgGylO2lK9qFGEsyTKnmnBjNTmvs2Uq48sIIPk0xFUiHNU8RVuMclp0g2heHPhU06h4VCpZy%2b182915OKDHpvP%2baWcGfCHR8o9fVaDDwkKVykHs7N%2fUmZMjHY2o0Z1YXKRICXvOMWWr6kS6zfpbuDsMvSj520R8QqNJWzb7u7yoqlvb69knZ08CvS5%2b6Xq7nac5SGJYnzLwEISnOIH0L0R2Cnoui67Z%2bQeHxdcKj6QNQ4u84LUWt0KPxq3LQzMWhaba3m%2bhowHcmza3FnwoqYNVi48PnAbU6d9tLuhsCsIteoNb3IVkMhNdsYpGAj2GrAxqD66Wj9a%2bUyMuj%2fm2wy8JHNNadts58DbzEsCsIVWG9OmM9O6mpSG%2fvHVRTvQ4GNqZrGZStfVP6MiC2%2bOLGUCrglx3tmCa9dq27GulH0%2fcuErr3Wdjn%2f69GT6h3FUUG9HDtOaLyz5WqMQpDz%2bUhXBJmVJ5UyIXYwz8yxGwtWhc3MRLABMrXzyEBtmfOAcC2sbWG%2filiYMueg%2bgo9qqpVeD4nPVErgFNGwgJPu00Dm786z9jgxd0hmFEKclEcdD%2fq7QAIPRU%3d&sign=QoojIJwusHut%2f8e3wDNB%2bDl%2bpTq60EZKmpaQPPdEIn9wgWWsNvWJF1h4wcvnJ%2fSyytZYCqv%2fS9Ok5tYNEU0iiZZwy70CqwC1DICinwSJt0o2n42tDEFOFovHhxmpqtEMJzgH9laubwf8BFIy95wMd3AmJuvqv1it1IXpMnfooq4fcq9FAxJK5xIZdGeZXNnoGn9CV%2bGKv%2f0CCOXJdqRSVFjXBRzVKh%2fMSjz2Q5RSbd%2fJb1IQsbBUg4SlGSjP1QvtD0psQcMa%2bLbDRusZZyIgjzfe4FOhwgpxrhbcpLqxlf048LRFswPdyp%2bMb06MPcC48NrKXZbjBVhxvYyKdYeONA%3d%3d";

        String b = URLDecoder.decode(Notify,"UTF-8");


        Map<String, String> map = getURLParameters(b);

        String encrypt_data = map.get("encrypt_data");
        String sign = map.get("sign");


        SignEncrypt signEncrypt = new SignEncrypt();
        // 使用类名和常量名来访问常量
        String publicKeystr = MyKey.publicKeystr;
        String privateKeystr = MyKey.privateKeystr;
        //密钥配置
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        byte[] publicKeyPath = Base64.decode(publicKeystr.getBytes());
        byte[] privateKeyPath = Base64.decode(privateKeystr.getBytes());
        PublicKey heepayPublicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyPath));
        PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyPath));

        String encryptData = signEncrypt.decrypt(encrypt_data,privateKey);


        if (signEncrypt.verify(encryptData, sign, heepayPublicKey)) {
            System.out.println("验签成功");
            //记得返回ok给汇元服务器
        }

    }


    public KjNotify() throws Exception {
    }
}
